package cn.gailvlun.gll.presentation.login.viewmodel

import android.app.Activity
import android.content.Intent
import android.databinding.BaseObservable
import android.databinding.Bindable
import android.view.View
import android.widget.TextView
import cn.gailvlun.gll.net.BR
import cn.gailvlun.gll.net.HttpMethods
import cn.gailvlun.gll.net.HttpSubscriber
import cn.gailvlun.gll.net.base.Profile
import cn.gailvlun.gll.net.login.ThirdPartyLoginReq
import cn.gailvlun.gll.presentation.base.BaseActivity
import cn.gailvlun.gll.presentation.login.CompleteProfileActivity
import cn.gailvlun.gll.presentation.login.PhoneBindingActivity
import cn.gailvlun.gll.presentation.login.SelectCountryCodeActivity
import cn.gailvlun.gll.presentation.login.adapter.LoginType
import cn.gailvlun.gll.presentation.login.bean.CountryCode
import cn.gailvlun.gll.presentation.main.MainActivity
import cn.gailvlun.gll.presentation.splash.LoginStateEnum
import cn.gailvlun.gll.util.*


open class BaseViewModel(private val mActivity: BaseActivity) : BaseObservable() {

    var countryNameAndCode: String = "CN +86"
        @Bindable
        get() {
            return field
        }
        set(value) {
            field = value
            notifyPropertyChanged(BR.countryNameAndCode)

        }

    private var thirdLogin: BaseThirdLogin? = null

    fun onSelectCountryCode(view: View) {
        val textView = view as TextView
        SelectCountryCodeActivity.openActivity(mActivity, textView.text.toString())
                .subscribe { activityResult ->
                    if (activityResult.resultCode() == Activity.RESULT_OK) {
                        val data = activityResult.data()
                        if (data != null) {
                            val countryCode = data.getSerializableExtra("countryCode") as CountryCode
                            val indexStart = countryCode.countryName.indexOf("(")
                            val indexEnd = countryCode.countryName.indexOf(")")
                            countryNameAndCode = "${countryCode.countryName.substring(indexStart + 1, indexEnd)} ${countryCode.countryCode}"
                        }
                    }
                }

    }

    fun on3rdPartyLogin(loginType: LoginType) {
        thirdLogin = when (loginType) {
            LoginType.qq -> QQLogin(mActivity)
            LoginType.wechat -> WechatLogin(mActivity)
            LoginType.weibo -> null
            else -> {
                null
            }
        }
        if (!thirdLogin!!.isAppInstalled) {
            ToastUtil.showShort("未安装应用")
            return
        }
        thirdLogin?.setOnLoginListener(object : OnLoginListener {
            override fun onSuccess(code: String?) {
                val dialog = DialogUtil.showIndeterminate(mActivity)
                HttpMethods.getLoginService()
                        .thirdPartyLogin(ThirdPartyLoginReq(loginType.backEndName, code))
                        .compose(RxUtil.applyScheduler())
                        .compose<Profile>(mActivity.bindToLifecycle<Profile>())
                        .subscribe(object : HttpSubscriber<Profile>() {
                            override fun onSuccess(response: Profile) {
                                dialog.dismiss()

                                ProfileUtil.save(response)



                                when (LoginStateEnum.getState(response)) {
                                    LoginStateEnum.NONE, LoginStateEnum.THIRD_PARTY_AUTHED_NO_BOUND -> {
                                        PhoneBindingActivity.openActivity(mActivity)
                                    }
                                    LoginStateEnum.MOBILE_BOUND -> {
                                        CompleteProfileActivity.openActivity(mActivity)
                                    }
                                    LoginStateEnum.ACTIVITED -> {
                                        MainActivity.openActivity(mActivity, false, true)
                                    }
                                }


                            }

                            override fun onFailure(errMsg: String, response: Profile?, code: Int) {
                                dialog.dismiss()
                                DialogUtil.showError(mActivity, errMsg)
                            }
                        })

            }

            override fun onFailure(errMsg: String?) {
                ToastUtil.showShort(errMsg)
            }
        })
        thirdLogin?.login()
    }

    fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        thirdLogin?.onActivityResult(requestCode, resultCode, data)

    }

}
